package com.lh.config;

import com.lh.realm.MyShiroRealm;
import com.lh.filter.BearerTokenFilter;
import org.apache.shiro.authc.BearerToken;
import org.apache.shiro.mgt.DefaultSessionStorageEvaluator;
import org.apache.shiro.mgt.DefaultSubjectDAO;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;
import java.util.HashMap;
import java.util.Map;

@Configuration
public class ShiroConfig {

    /**
     * 注入Realm
     */
    @Bean
    public MyShiroRealm myShiroRealm() {
        return new MyShiroRealm();
    }

    /**
     * 配置SecurityManager
     */
    @Bean
    public DefaultWebSecurityManager securityManager(MyShiroRealm realm) {
        // 将realm放进入
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        // 在这里需要先设置支持的AuthenticationToken 默认是UsernamePasswordToken 这里需要用BearerToken
        realm.setAuthenticationTokenClass(BearerToken.class);
        securityManager.setRealm(realm);

        // 禁用session 因为使用token实现 不需要session存储用户信息
        DefaultSubjectDAO dao = new DefaultSubjectDAO();
        DefaultSessionStorageEvaluator defaultSessionStorageEvaluator = new DefaultSessionStorageEvaluator();
        defaultSessionStorageEvaluator.setSessionStorageEnabled(false);
        dao.setSessionStorageEvaluator(defaultSessionStorageEvaluator);

        // 将sessiondao放入SecurityManager
        securityManager.setSubjectDAO(dao);

        return securityManager;
    }

    /**
     * 配置ShiroFilterFactoryBean
     */
    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(DefaultWebSecurityManager securityManager) {
        // 先设置安全管理器 SecurityManager
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager);
        shiroFilterFactoryBean.setLoginUrl("/login");

        // 添加自定义过滤器
        Map<String, Filter> filters = new HashMap<>();
        filters.put("tokenFilter", new BearerTokenFilter());
        shiroFilterFactoryBean.setFilters(filters);

        // 自定义过滤规则
        Map<String, String> filterChainDefinitionMap = new HashMap<>();
        // 放行swagger相关url
        filterChainDefinitionMap.put("/doc.html/**", "anon");
        filterChainDefinitionMap.put("/swagger-resources/**", "anon");
        filterChainDefinitionMap.put("/v2/**", "anon");
        filterChainDefinitionMap.put("/webjars/**", "anon");
        // 其余的都要过BearerTokenFilter 过滤器
        filterChainDefinitionMap.put("/**", "tokenFilter");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);

        return shiroFilterFactoryBean;
    }

    /**
     * Shiro开启权限注解切点扫描
     */
    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(DefaultWebSecurityManager defaultWebSecurityManager) {
        AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
        authorizationAttributeSourceAdvisor.setSecurityManager(defaultWebSecurityManager);
        return authorizationAttributeSourceAdvisor;
    }

}
